package com.ysd.lis.service.callNum.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.callNum.CallSampleVsFeeOrSam;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.callNum.CallSampleVsFeeOrSamMapper;
import com.ysd.lis.service.callNum.CallSampleVsFeeOrSamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-28
 */
@Service
public class CallSampleVsFeeOrSamServiceImpl extends ServiceImpl<CallSampleVsFeeOrSamMapper, CallSampleVsFeeOrSam> implements CallSampleVsFeeOrSamService {

    @Override
    public Result getItemList(CallSampleVsFeeOrSam callSampleVsFeeOrSam) {
        MPJLambdaWrapper<CallSampleVsFeeOrSam> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(CallSampleVsFeeOrSam.class);
        if (callSampleVsFeeOrSam.getType().equals("1")){
            wrapper.selectAs(LabSampleType::getSampleTypeName, CallSampleVsFeeOrSam::getName);
            wrapper.leftJoin(LabSampleType.class,  it->it.eq(LabSampleType::getSampleTypeCode, CallSampleVsFeeOrSam::getCode).eq(LabSampleType::getDelFlag, "0"));
        }else {
            wrapper.selectAs(LabFeeItem::getFeeName, CallSampleVsFeeOrSam::getName);
            wrapper.leftJoin(LabFeeItem.class,  it->it.eq(LabFeeItem::getFeeCode, CallSampleVsFeeOrSam::getCode).eq(LabFeeItem::getDelFlag, "0"));
        }
        wrapper.eq(CallSampleVsFeeOrSam::getSampleTypeCode, callSampleVsFeeOrSam.getSampleTypeCode());
        wrapper.eq(CallSampleVsFeeOrSam::getType, callSampleVsFeeOrSam.getType());
        wrapper.eq(CallSampleVsFeeOrSam::getDelFlag, "0");
        List<CallSampleVsFeeOrSam> list = this.list(wrapper);
        return Result.succ(1,  "查询成功", list);
    }

    @Override
    public Result addSampleTypeCode(CallSampleVsFeeOrSam callSampleVsFeeOrSam) {
        if (StringUtils.isBlank(callSampleVsFeeOrSam.getSampleTypeCode())){
            return Result.fail("请选择项目！");
        }
        LambdaQueryWrapper<CallSampleVsFeeOrSam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CallSampleVsFeeOrSam::getSampleTypeCode, callSampleVsFeeOrSam.getSampleTypeCode());
        wrapper.eq(CallSampleVsFeeOrSam::getType, callSampleVsFeeOrSam.getType());
        wrapper.eq(CallSampleVsFeeOrSam::getCode, callSampleVsFeeOrSam.getCode());
        wrapper.eq(CallSampleVsFeeOrSam::getDelFlag, "0");
        boolean save = this.saveOrUpdate(callSampleVsFeeOrSam, wrapper);
        if (save) {
            return Result.succ(1, "保存成功!", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result deleteSampleCode(CallSampleVsFeeOrSam callSampleVsFeeOrSam) {
        int i = this.baseMapper.deleteById(callSampleVsFeeOrSam.getId());
        if (i > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
